package ProducerConsumer_Mantou;

/**
 * Created by key on 2015/3/1.
 * 共享栈空间
 */
public class StackBasket{
    Mantou mt[] = new Mantou[6];
    int index;

    public synchronized void push(Mantou m){
        try{
            while(index == mt.length){
                System.out.println("full");
                this.wait();
            }
            this.notify();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }catch (IllegalMonitorStateException e){
            e.printStackTrace();
        }

        mt[index]= m;
        index++;
        System.out.println("生产了：" + m + "个" + "--总共：" + index);
    }


    public synchronized Mantou pop(){
        try{
            while(index == 0){
                System.out.println("null");
                this.wait();
            }
            this.notify();
        }catch(InterruptedException e){
            e.printStackTrace();
        }catch(IllegalMonitorStateException e){
            e.printStackTrace();
        }
        index--;
        System.out.println("消费了："+mt[index]+"---总共："+index);
        return mt[index];
    }
}
